/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hss01248.dialog.comp;

import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.Tool;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

public class AlertDialog extends CommonDialog {
    public static final int STYLE_SINGLE = 0;
    public static final int STYLE_MULTI = 1;
    private Button cancelBtn;
    private Button okBtn;
    private Button neutralBtn;
    private OnDismissListener dismissListener;
    private OnShowListener showListener;

    /**
     * get positiveButton
     * @return  button
     */
    public Button getPositiveButton()
    {
        return okBtn;
    }

    /**
     * get negative button
     * @return button
     */
    public Button getNegativeButton()
    {
        return cancelBtn;
    }

    /**
     * get neutral button
     * @return button
     */
    public Button getNeutralButton()
    {
        return neutralBtn;
    }

    /**
     * set positive button
     * @param btn button
     */
    protected void setPositiveButton(Button btn)
    {
        okBtn = btn;
    }

    /**
     * set negative button
     * @param btn button
     */
    protected void setNegativeButton(Button btn)
    {
        cancelBtn = btn;
    }

    /**
     * set neutral button
     * @param btn button
     */
    protected void setNeutralButton(Button btn)
    {
        neutralBtn = btn;
    }
    protected AlertDialog(Context context) {
        super(context);
    }

    /**
     * set onShow listener
     * @param listener listener
     */
    public void setOnShowListener( OnShowListener listener)
    {
        showListener = listener;
    }

    /**
     * on destroy function
     */
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (dismissListener != null)
        {
            dismissListener.onDismiss();
        }
    }

    /**
     * onShow function
     */
    @Override
    protected void onShow() {
        if (showListener != null)
        {
            showListener.onShow();
        }
        super.onShow();
    }
    public interface OnDismissListener
    {
        void onDismiss();
    }
    public interface OnShowListener {
        void onShow();
    }
    public static class Builder {
        private Component rootView;
        private ScrollView sv;
        private DirectionalLayout listContainer;

        private Text titleText;
        private Text messageText;
        private DirectionalLayout custumerView;
        private Context ctx;
        private OnDismissListener dismissListener;
        private Button cancelBtn;
        private Button okBtn;
        private Button neutralBtn;
        public Builder(Context context)
        {
            ctx = context;
            LayoutScatter inflater = LayoutScatter.getInstance(ctx);
            rootView = inflater.parse(ResourceTable.Layout_alertdialog, null, false);
            messageText = (Text)rootView.findComponentById(ResourceTable.Id_alert_msg_text);
            messageText.setVisibility(Component.HIDE);
            titleText = (Text)rootView.findComponentById(ResourceTable.Id_alert_title);
            titleText.setVisibility(Component.HIDE);
            custumerView = (DirectionalLayout)rootView.findComponentById(ResourceTable.Id_alert_custum_view);
            custumerView.setVisibility(Component.HIDE);
            sv = (ScrollView) rootView.findComponentById(ResourceTable.Id_alertsv);
            listContainer = (DirectionalLayout) rootView.findComponentById(ResourceTable.Id_alertlist);
            cancelBtn = (Button) rootView.findComponentById(ResourceTable.Id_alertcancelbtn);
            okBtn = (Button) rootView.findComponentById(ResourceTable.Id_alertokbtn);
            neutralBtn = (Button) rootView.findComponentById(ResourceTable.Id_alertneutralbtn);
            cancelBtn.setVisibility(Component.HIDE);
            okBtn.setVisibility(Component.HIDE);
            neutralBtn.setVisibility(Component.HIDE);
        }

        /**
         * set custum view
         * @param custumView view component
         */
        public void setView(Component custumView)
        {
            if (custumView == null)
            {
                return;
            }
            custumerView.addComponent(custumView);
            custumerView.setVisibility(Component.VISIBLE);
            sv.setVisibility(Component.HIDE);
            listContainer.setVisibility(Component.HIDE);
            //listContainer.removeAllComponents();
            //listContainer.addComponent(custumView);
        }

        /**
         * set message
         * @param message message
         */
        public void setMessage(String message)
        {
            if (Tool.isEmpty(message)) {
                return;
            }
            sv.setVisibility(Component.HIDE);
            listContainer.setVisibility(Component.HIDE);
            messageText.setText(message);
            messageText.setMultipleLine(true);
            messageText.setVisibility(Component.VISIBLE);
        }

        /**
         * set positive button text and listener
         * @param text text
         * @param listeners listener
         */
        public void setPositiveButton(String text, Component.ClickedListener listeners)
        {
            if (Tool.isEmpty(text))
            {
                okBtn.setVisibility(Component.HIDE);
            }
            else
            {
                okBtn.setText(text);
                okBtn.setVisibility(Component.VISIBLE);
                okBtn.setClickedListener(listeners);
            }
        }

        /**
         * set dismiss listener
         * @param listener listener
         */
        public void setOnDismissListener(OnDismissListener listener)
        {
            dismissListener = listener;
        }
        /**
         * set negative button text and listener
         * @param text text
         * @param listeners listener
         */
        public void setNegativeButton(String text, Component.ClickedListener listeners)
        {
            if (Tool.isEmpty(text))
            {
                cancelBtn.setVisibility(Component.HIDE);
            }
            else
            {
                cancelBtn.setText(text);
                cancelBtn.setVisibility(Component.VISIBLE);
                cancelBtn.setClickedListener(listeners);
            }
        }
        /**
         * set neutral button text and listener
         * @param text text
         * @param listeners listener
         */
        public void setNeutralButton(String text, Component.ClickedListener listeners)
        {
            if (Tool.isEmpty(text))
            {
                neutralBtn.setVisibility(Component.HIDE);
            }
            else
            {
                neutralBtn.setText(text);
                neutralBtn.setVisibility(Component.VISIBLE);
                neutralBtn.setClickedListener(listeners);
            }
        }

        /**
         * set single choice items
         * @param items display items
         * @param defaultChoose default choose
         * @param listener checked state listener
         */
        public void setSingleChoiceItems(String[] items, int defaultChoose, RadioContainer.CheckedStateChangedListener listener)
        {
            RadioContainer container = new RadioContainer(ctx);
            for(String item : items)
            {
                RadioButton radio = new RadioButton(ctx);
                radio.setText(item);
                radio.setPaddingLeft(50);
                radio.setPaddingTop(10);
                StateElement stE = new StateElement();
                stE.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new ShapeElement(ctx, ResourceTable.Graphic_background_radio_checked));
                stE.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(ctx, ResourceTable.Graphic_background_radio_empty));
                radio.setButtonElement(stE);
                radio.setTextSize(14, Text.TextSizeType.FP);
                container.addComponent(radio);
            }
            container.mark(defaultChoose);
            container.setMarkChangedListener(listener);
            listContainer.addComponent(container);
        }

        /**
         * set multi choice items
         * @param items display items
         * @param checkedItems default checked items
         * @param listener checked state listener
         */
        public void setMultiChoiceItems(String[] items, boolean[] checkedItems, AbsButton.CheckedStateChangedListener listener) {
            for(int i = 0 ; i<items.length; i++)
            {
                Checkbox checkbox = new Checkbox(ctx);
                checkbox.setText(items[i]);
                checkbox.setId(i);
                checkbox.setPaddingLeft(50);
                checkbox.setMarginsLeftAndRight(10, 10);
                checkbox.setPaddingTop(10);
                checkbox.setTextSize(14, Text.TextSizeType.FP);
                checkbox.setChecked(checkedItems[i]);
                checkbox.setCheckedStateChangedListener(listener);
                StateElement stE = new StateElement();
                stE.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new ShapeElement(ctx, ResourceTable.Graphic_background_checkbox_checked));
                stE.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new ShapeElement(ctx, ResourceTable.Graphic_background_checkbox_empty));
                checkbox.setButtonElement(stE);
                listContainer.addComponent(checkbox);
            }
        }

        /**
         * set title
         * @param title title
         * @param titleColor color
         */
        public void setTitle(String title, int titleColor)
        {
            if (Tool.isEmpty(title))
            {
                return;
            }
            titleText.setText(title);
            titleText.setMultipleLine(true);
            titleText.setVisibility(Component.VISIBLE);
            Tool.setTextColorById(titleText, titleColor);
        }

        /**
         * create alert dialog
         * @return alert dialog
         */
        public AlertDialog create()
        {
            AlertDialog dialog = new AlertDialog(ctx);
            dialog.setContentCustomComponent(rootView);
            dialog.setPositiveButton(okBtn);
            dialog.setNegativeButton(cancelBtn);
            dialog.setNeutralButton(neutralBtn);
            dialog.dismissListener = dismissListener;
            return dialog;
        }

    }
}
